/*
 * @Author: czy
 * @Date: 2021-04-04 22:39:40
 * @LastEditTime: 2021-05-02 08:07:21
 */
#include "io.h"
#include "uart.h"

/**
 * @description: Output a character to the uart.
 * @param c Character to be output.
 */
void uart_put_char(unsigned char c)
{
    switch (c)
    {
    case 127:
        outb(uart_base, '\b');
        outb(uart_base, ' ');
        outb(uart_base, '\b');
        break;
    case '\n':
        outb(uart_base, '\n');
        outb(uart_base, '\r');
        break;
    default:
        outb(uart_base, c);
        break;
    }
}

/**
 * @description: Get a character from the uart.
 * @return Character entered.
 */
unsigned char uart_get_char(void)
{
    while (!(inb(uart_flag) & 1))
        ;
    return inb(uart_base);
}

/**
 * @description: Output a string to the uart.
 * @param str   String to be output.
 */
void uart_put_chars(char *str)
{
    int i = 0;
    do
    {
        uart_put_char(str[i]);
    } while (str[++i] != '\0');
}

/**
 * @description: Cursor offset.
 * @param offset    Offset.
 */
void uart_offset(int offset)
{
    if (offset > 0)
    {
        for (int i = 0; i < offset; i++)
        {
            uart_put_char(27);
            uart_put_char(91);
            uart_put_char(67);
        }
    }
    else
    {
        for (int i = 0; i < -offset; i++)
        {
            uart_put_char(27);
            uart_put_char(91);
            uart_put_char(68);
        }
    }
}